package com.book.controller;

import com.book.entity.Cart;
import com.book.entity.User;
import com.book.service.AddressService;
import com.book.service.CartService;
import com.book.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cart")
public class CartController extends BaseController {
	@Autowired
	private CartService cartService;
	@Autowired
	private AddressService addressService;
	private String showPath="/resource/showImg?path=";
	@RequestMapping("/list")
	public ModelAndView list(Cart cart, HttpServletRequest request, HttpServletResponse response, ModelMap m){
//		String showImgUrl=request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+request.getContextPath()+showPath;
		ModelAndView mav=new ModelAndView();
		String showImgUrl="";
		mav.addObject("carts", cartService.getCartsByUId(SecurityUtil.getCurrentUser().getUId()));
		mav.addObject("addresses",addressService.getAddressByUsername(SecurityUtil.getCurrentUser().getUsername()));
		mav.addObject("showImgUrl",showImgUrl);
		mav.setViewName("cart");
		return mav;
	}
	@RequestMapping("/statistics")
	@ResponseBody
	public Map<String,Object> statistics(HttpServletRequest request, HttpServletResponse response){
//		User user=SecurityUtil.getCurrentUser();
		User user=(User) request.getSession().getAttribute("currentUser");
		Map<String,Object> result=new HashMap<String, Object>();
		if(user==null){
			result.put("status", false);
			result.put("msg", "用户不存在");
		}
		Map<String,Object> statisticResult=cartService.statistics(user.getUId());
		result.put("statistic", statisticResult);
		return result;
	}
	@RequestMapping("/save")
	@ResponseBody
	public Map<String,Object> save(Cart cart, HttpServletRequest request, HttpServletResponse response){
		Map<String,Object> result=new HashMap<String, Object>();
		try{
			cartService.insert(cart);
			msg.setSuccessMsg("添加成功");
		}catch(Exception e){
			msg.setErrorMsg("添加失败");
		}
		result.put("msg", msg);
		return result;
	}
	@RequestMapping("/remove")
	@ResponseBody
	public Map<String,Object> remove(HttpServletRequest request, HttpServletResponse response){
		Map<String,Object> result=new HashMap<String, Object>();
		String id=get(request, "id");
		if(StringUtils.isEmpty(id)){
			msg.setErrorMsg("id不能为空");
			result.put("msg", msg);
			return result;
		}
		try{
			cartService.deleteById(id);
			msg.setSuccessMsg("删除成功");
		}catch(Exception e){
			msg.setErrorMsg("删除失败");
			return result;
		}
		result.put("msg", msg);
		return result;
	}
}
